package com.qeng.aiwanba.util;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.alipay.sdk.app.EnvUtils;
import com.alipay.sdk.app.PayTask;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.tencent.mm.opensdk.modelpay.PayReq;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by lixy on 2017/10/9.
 */

public class PayUtil {
    private static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                String code = (String)msg.obj;
                if(code.equals("9000")){
                    jsCallback.onCallBack(String.valueOf(Constants.PAY_SUCCESS_CODE));
                }else if(code.equals("4000")){
                    jsCallback.onCallBack(String.valueOf(Constants.PAY_FAILED_CODE));
                }else if(code.equals("6001")){
                    jsCallback.onCallBack(String.valueOf(Constants.PAY_CANCEL_CODE));
                }else{
                    jsCallback.onCallBack(String.valueOf(Constants.PAY_OTHER_CODE));
                }
            }
            super.handleMessage(msg);
        }
    };

    private static CallBackFunction jsCallback;

    public static CallBackFunction getJsCallback(){
        return jsCallback;
    }

    public static void wechatPay(Context context, String data, CallBackFunction callback){
        jsCallback = callback;

        try {
            JSONObject json = new JSONObject(data);

            PayReq request = new PayReq();
            request.appId = json.optString("appid");
            request.partnerId = json.optString("partnerid");
            request.prepayId= json.optString("prepayid");
            request.packageValue = json.optString("package");
            request.nonceStr= json.optString("noncestr");
            request.timeStamp= json.optString("timestamp");
            request.sign= json.optString("sign");
            MyWxApiManager.getInstance().getIwxapi(context).sendReq(request);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void alipay(final Context context,final String orderInfo,final CallBackFunction callback){
        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);

        jsCallback = callback;

        new Thread(new Runnable(){
            @Override
            public void run() {
                PayTask alipay = new PayTask((Activity) context);
                Map<String,String> map = alipay.payV2(orderInfo,true);

                JSONObject object = new JSONObject(map);
                DebugFlag.logVerbose(PayUtil.class.getSimpleName(),"alipay result :"+object.toString());
                String code = object.optString("resultStatus");
                Message message = new Message();
                message.what= 0;
                message.obj = code;
                handler.sendMessage(message);
            }
        }).start();
    }
}
